// pages/mine/mine.js
let utils = require("../../utils/utils.js");
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		// 5-8
		aboutMe: false, //关于我们默认隐藏
		// star 5-7
		model: false, //是否展示密码输入层 
		pwdVal: '', //获取输入的密码
		payFocus: true, //文本框焦点
		id: 1,
		password: '', //存放第一次输入的密码
		// end 5-7
		myPassword: "",
		motto: 'Hello World',
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo')
			/* my-glass--s */
			,
		typeindex: 2,
		headImg: '',
		mylinks: [{
				link: '/base/wallet',
				icon: 'icon-qianbao',
				name: '我的钱包',
				arrow: 'icon-youjian'
			},
			{
				link: '/base/deposit',
				icon: 'icon-yajinguanli',
				name: '我的押金',
				arrow: 'icon-youjian'
			},
			{
				link: '/base/card',
				icon: 'icon-yueqia',
				name: '月卡',
				arrow: 'icon-youjian'
			},
			{
				link: '/base/discountCoupon',
				icon: 'icon-youhuiquan',
				name: '我的优惠券',
				arrow: 'icon-youjian'
			}
		],
		customlinks: [{
				link: '#',
				icon: 'icon-kefu',
				name: '联系客服',
				arrow: 'icon-youjian'
			},
			{
				link: '/base/question',
				icon: 'icon-wenti',
				name: '常见问题',
				arrow: 'icon-youjian'
			},
			{
				link: '#',
				icon: 'icon-guanyuwomen',
				name: '关于我们',
				arrow: 'icon-youjian'
			}
		],
		userName: '',
		getMycashPledgeUrl: '/myCashPledge/meConter'
			/*/myCashPledge/mycashPledge*/
			,
		depositInfo: {
			mydeposit: ''
		},
		aboutMe: false, //关于我们默认隐藏
		getAboutUrl: '/aboutUs/get',
		aboutList: [],
		cashPledgeId: '',
		isMonthCard: '',
		footers: [{
					icon: 'icon-tianchongxing-',
					name: '首页'
				},
				{
					icon: 'icon-dingdan1',
					name: '订单'
				},
				{
					icon: 'icon-wode1',
					name: '我的'
				}
			]
			/* my-glass--d */
			,
		offering: []
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let that = this;
		
		let userInfo = wx.getStorageSync('userInfo');
		let indexData = wx.getStorageSync('indexData');
		let userData = indexData.user;
		that.getCashData();
		if (userInfo || userData) {
			that.setData({
				headImg: userData.userImage || userInfo.headimgurl,
				userName: userData.userName || userInfo.nickname
			});
		} else {
			wx.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
						wx.getUserInfo({
							success: res => {
								// 可以将 res 发送给后台解码出 unionId
								console.log(res);
								let avatarUrl = res.userInfo.avatarUrl;
								let nickName = res.userInfo.nickName;
								that.setData({
									headImg: avatarUrl,
									userName: nickName
								})
								// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
								// 所以此处加入 callback 以防止这种情况
								if (this.userInfoReadyCallback) {
									this.userInfoReadyCallback(res)
								};
							}
						})
					}
				}
			})
		}
	},
	cardtips(){
		let monthCardInfo=wx.getStorageSync('monthCardInfo');
		if(monthCardInfo){
			utils.tips(monthCardInfo);
			wx.removeStorageSync('monthCardInfo');
		};
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
	},
	hiddenMe: function() {
		let that = this;
		that.data.aboutMe = false;
		that.setData({
			aboutMe: that.data.aboutMe
		})
	},
	varieryToLink: function(e) {
		let index = e.currentTarget.dataset.index;
		if (index == 0) {
			wx.navigateTo({
				url: '../wallet/wallet'
			});
		} else if (index == 1) {
			wx.navigateTo({
				url: '../deposit/deposit'
			});
		} else if (index == 2) {
			/* if (Number(this.data.isMonthCard) === 1) {
				utils.tips('您所在的影院设备未开启月卡功能！');
				return false
			}; */
			if (Number(this.data.cashPledgeId) === 0 || !this.data.cashPledgeId || this.data.cashPledgeId == null) {
				utils.tips('您还没有缴纳押金，需要缴纳押金才能购买月卡！');
				return false
			} else {
				wx.navigateTo({
					url: '../card/card'
				});
			};

		} else if (index == 3) {
			wx.navigateTo({
				url: '../discountCoupon/discountCoupon'
			});
		};
	},
	questions: function() {
		wx.navigateTo({
			url: '../question/question'
		});
	},
	aboutme(){
		let that = this;
		that.data.aboutMe = true;
		let deviceCode = wx.getStorageSync('deviceCode');
		let _data = {
			url: that.data.getAboutUrl,
			data: {
				deviceCode: deviceCode
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res);
			if (res.data.status == 200) {
				that.data.aboutList = res.data.data;
				console.log(that.data.aboutList)
				that.setData({
					aboutList: that.data.aboutList,
					aboutMe: that.data.aboutMe
				})
			}
		});
		
	},
	getCashData: function() {
		let that = this;
		let indexData = wx.getStorageSync('indexData');
		let userData = indexData.user;
		let _data = {
			url: that.data.getMycashPledgeUrl,
			methods: 'post',
			nonelist: true,
			data: {
				'userId': userData.userId,
				'type': userData.type,
				'deviceId': userData.deviceCode
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res);
			that.data.cashPledgeId = res.data.data.user.cashPledgeId;
			//this.data.isMonthCard = res.data.data.cinema.isMonthCard;
			let availableAntecedentMoney = res.data.data.user.availableAntecedentMoney;
			//sessionStorage.setItem('user', JSON.stringify(res.data.data));
			let cashPledge = res.data.data.cashPledge;
			if (!availableAntecedentMoney) {
				that.data.depositInfo.mydeposit = '0.00';
			} else {
				that.data.depositInfo.mydeposit = res.data.data.user.availableAntecedentMoney;
			};
			that.setData({
				depositInfo: that.data.depositInfo,
			})
			this.cardtips();
		});
	},
	/* 支付  */
	pay(param) {
		let that = this;
		wx.requestPayment({
			timeStamp: param.timeStamp,
			nonceStr: param.nonceStr,
			package: param.package,
			signType: param.signType,
			paySign: param.paySign,
			success: function(res) {
				//return false;
				if (res.errMsg == "requestPayment:ok") { // 调用支付成功
					that.getuseravater(); /* 获取个人信息 */
					wx.showToast({
						title: '已充值成功',
						icon: 'none'
					})

				} else if (res.errMsg == 'requestPayment:cancel') {
					wx.showToast({
						title: '已取消充值',
						icon: 'none'
					})
				}
			},
			fail: function(res) {
				// fail
			},
			complete: function() {
				// complete
			}
		})
	},
	/* 导航页跳转*/
	pageLinks: function(e) {
		let index = e.currentTarget.dataset.index;
		console.log(0);
		if (index == 0) {
			wx.reLaunch({
				url: '../index/index'
			});
		} else if (index == 1) {
			wx.reLaunch({
				url: '../myorder/myorder'
			});
		} else {
			return false;
		};
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		let sharepages = app.globalData.sharepage
		return sharepages;
	}

})
